Java 类中静态初始化

先看一下结果···
PIC
点开有详细解释哦~

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class father{
public int f = 1;
father(){
show();
//show() 此时被子类重写
//且子类中未显示初始化,故打印"son block---0"
}
void show(){
System.out.println("father---"+f);
}
static{
System.out.println("father static block---");
}
{
System.out.println("father block---"+f);
}
}
class son extends father{
public int s = 2;
son(){
super();//第一步
//第二部:显示初始化,为 s 初始化
//第三部:构造代码块初始化,打印 "son block---2"
show();
}
void show(){
System.out.println("son---"+s);
}
static{
System.out.println("son static block---");
}
{
System.out.println("son block---"+s);
}
}
class test{
public static void main(String[] args){
new son();
}
}